/*
 * Global Variables 
 * (that could be accessed inside all functions 
 * without the need to be passed as arguments.)
 */

var APP_CONTEXT_URL = "http://localhost:8080/downloader";

var gDepartment = null;
var gSemester = -1;
var gYear = null;
var gXHR = null;

function showProgress() {
	document.getElementById("pbar1").style.visibility = "visible";
	document.getElementById("pbar2").style.visibility = "visible";
}

function hideProgress() {
	document.getElementById("pbar1").style.visibility = "hidden";
	document.getElementById("pbar2").style.visibility = "hidden";
}

/*
 * Utility to get the XMLHttpRequest Object in a cross browser compatible way.
 * 
 * It returns the newly instantiated XmlHttpRequest JavaScript Object as well as
 * assigns the same to the global variable gXHR to be used by the AJAX response
 * call back handlers.
 * 
 */
function getXmlHttpRequest()
{
	if (window.XMLHttpRequest) {
		// for Non-IE Browsers like Chrome, Firefox, Opera.
		gXHR = new XMLHttpRequest();
	} else {
		// for Internet Explorer.
		gXHR = new ActiveXObject("Microsoft.XMLHTTP");
	}
	return gXHR;
}

/*
 * Utility to remove the children of a given element.
 */
function removeChildren(widget)
{
	while (widget.firstChild) {
		widget.removeChild(widget.firstChild);
	}
}

/*
 * Utility to fire an AJAX GET Request to a URL accepting a success callback.
 */
function makeAjaxCall(url, responseCallbackFunction)
{
	var xhr = getXmlHttpRequest();
	xhr.onreadystatechange = responseCallbackFunction;
	xhr.open("GET", url, "true");
	xhr.send(null);
}

/*
 * Utility to check whether AJAX Response is a successful HTTP Response.
 */
function isXHRSuccess()
{
	return gXHR.readyState == 4 && gXHR.status == 200;
}

/*
 * AJAX Calls to Server Side.
 */

function fetchDepartments()
{
	showProgress();
	makeAjaxCall(APP_CONTEXT_URL + "/getDeptServlet", handleDepartmentsResponse);
}

function fetchYears()
{
	showProgress();
	makeAjaxCall(APP_CONTEXT_URL + "/getYears?dept=" + gDepartment, handleYearsResponse);
}

function fetchSemesters()
{
	showProgress();
	makeAjaxCall(APP_CONTEXT_URL + "/getSemServlet?year=" + gYear, handleSemestersResponse);
}

function fetchSubjects()
{
	showProgress();
	makeAjaxCall(APP_CONTEXT_URL + "/getSubjects?sem=" + gSemester + "&&dept=" + gDepartment, handleSubjectsResponse);
}

/*
 * AJAX Response Handlers
 */
function handleDepartmentsResponse()
{
	if (isXHRSuccess()) {

		var deptCombo = document.getElementById("deptCombobox");
		removeChildren(deptCombo);

		var departments = gXHR.responseText.split(",");
		for ( var i in departments) {
			deptCombo.appendChild(document.createElement("option"));
			deptCombo.lastChild.innerHTML = departments[i];
			deptCombo.addEventListener("click", onClickOfDepartmentCombo);
		}
		hideProgress();
	}
	
}

function handleYearsResponse()
{
	if (isXHRSuccess()) {

		var yearsCombo = document.getElementById("yearsCombobox");
		removeChildren(yearsCombo);

		var years = gXHR.responseText.split(",");
		for ( var i in years) {
			yearsCombo.appendChild(document.createElement("option"));
			yearsCombo.lastChild.innerHTML = years[i];
			yearsCombo.addEventListener("change", onClickOfYearCombo);
		}
		hideProgress();
	}
}

function handleSemestersResponse()
{
	if (isXHRSuccess()) {

		var semesterCombo = document.getElementById("semCombobox");
		removeChildren(semesterCombo);

		var semesters = gXHR.responseText.split(",");
		for ( var i in semesters) {
			semesterCombo.appendChild(document.createElement("option"));
			semesterCombo.lastChild.innerHTML = semesters[i];
			semesterCombo.addEventListener("click", onClickOfSemesterCombo);
		}
		hideProgress();
	}
}

function handleSubjectsResponse()
{
	if (isXHRSuccess()) {

		var subjectsHolder = document.getElementById("subjectsHolder");
		removeChildren(subjectsHolder);

		var subjects = gXHR.responseText.split(",");
		for ( var i in subjects) {
			subjectsHolder.appendChild(document.createElement("a"));
			subjectsHolder.lastChild.setAttribute("href", APP_CONTEXT_URL + "/download?semester=" + gSemester
					+ "&subject=" + subjects[i]);
			subjectsHolder.lastChild.innerHTML = subjects[i];
			subjectsHolder.appendChild(document.createElement("br"));
		}
		hideProgress();
	}

}

/*
 * HTML Widget Event Handlers.
 */
function onClickOfDepartmentCombo(event)
{
	event = event || window.event;
	var deptCombo = event.target || event.srcElement;
	// update the selected department to the global variable.
	gDepartment = deptCombo.value;

	// Now fire an AJAX call to populate the years based on the department
	// selected.
	fetchYears();
}

function onClickOfYearCombo(event)
{
	event = event || window.event;
	var yearCombo = event.target || event.srcElement;

	// Update the global variable with the selected year value.
	gYear = yearCombo.value;

	// Now fire an AJAX call to populate the Semesters based on the year
	// selected.
	fetchSemesters();
}

function onClickOfSemesterCombo(event)
{
	event = event || window.event;
	var semesterCombo = event.target || event.srcElement;

	// Update the global variable with the selected semester value.
	gSemester = semesterCombo.value;

	// Now fire an AJAX call to populate the Semesters based on the year
	// selected.
	fetchSubjects();
}

function bootstrap() {
	hideProgress();
}
window.onload = function() {
	initBar();
	bootstrap();
	fetchDepartments();
};
